using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace SharpCast
{
    [Serializable()]
    public abstract class Common
    {
        [NonSerialized()]
        protected XmlElement element;

        /// <summary>
        /// Returns the first child element with the specified tag from the parent
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="tag"></param>
        /// <returns></returns>
        public static XmlElement getChild(XmlElement parent, String tag)
        {
            XmlNodeList children = parent.GetElementsByTagName(tag);
            if (children.Count > 0)
                return (XmlElement)children[0];
            else
                return null;
                //throw new Exception("Unable to find child element with tag " + tag + " in " + parent.Name);
        }

        public XmlElement getChild(string tag)
        {
            return getChild(this.element, tag);
        }

        public string getValue(String tag) {
            try {
                return getChild(tag).InnerText;
            }
            catch {
                return "";
            }
        }
    }
}
